<?php
/**
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* 
* @copyright Copyright (c) 2008-2009 Ne8, LLC <josh.ribakoff@gmail.com>
* @license New BSD License
* @version 0.1
* @category Shuffler  
* @package Shuffler_Db 
*/
class Shuffler_Db_Field
{
    protected $name;
    protected $type;
    protected $len;
    protected $default;
    protected $extra;
    protected $null = false;
    protected $table;
    
    /**
    * name is immutable because its used as an array index in Table
    */
    public function __construct( Shuffler_Db_Table $table, $name )
    {
        $this->table = $table;
        $this->name = $name;
    }
    
    public function getName() { return $this->name; }
    
    public function setType( $type ) { $this->type = $type; return $this; }
    public function getType() { return $this->type; }
    
    public function hasLength()
    {
        if( $this->getType() == 'text' ) return false;
        return strlen( $this->len ) > 0;
    }
    public function setLength( $len ) { $this->len = $len; return $this; }
    public function getLength() { return $this->len; }
    
    public function setDefault( $default )
    {
        if( $this->getExtra() == 'auto increment' )
        {
            throw new Exception( 'Cannot set auto incrememnt plus a default value' );  
        }
        $this->default = $default;
        return $this;
    
    }
    public function getDefault() { return $this->default; }
    public function hasDefault() { return isset( $this->default ); }
    
    /**
    * Set wether field is NULLable
    * @param bool
    */
    public function setNull( $bool ) { $this->null = (bool)$bool; return $this; }
    public function getNull() { return $this->null; }
    
    public function setExtra( $extra )
    {
        if( $extra == 'auto increment' && $this->hasDefault() )
        {
            throw new Exception( 'Cannot set auto incrememnt plus a default value' );
        }
        $this->extra = $extra;
        return $this;
    }
    public function getExtra() { return $this->extra; }
    
    public function setPrimary() { $this->table->setPrimary( $this ); return $this; }
    
}